package org.jeecg.modules.storage.service.impl;

import org.jeecg.modules.procurement.entity.BussMaterialProcurementDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementMapper;
import org.jeecg.modules.storage.entity.BussStorageNoticeMaterial;
import org.jeecg.modules.storage.entity.PackageStorageMaterial;
import org.jeecg.modules.storage.mapper.BussStorageNoticeMapper;
import org.jeecg.modules.storage.mapper.BussStorageNoticeMaterialMapper;
import org.jeecg.modules.storage.mapper.PackageStorageMaterialMapper;
import org.jeecg.modules.storage.service.IProcurementManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author gbstart
 * @create 2024-08-13 10:28
 */
@Service
public class ProcurementManagementServiceImpl implements IProcurementManagementService {


    @Autowired
    private BussMaterialProcurementMapper bussMaterialProcurementMapper;

    @Autowired
    private BussMaterialProcurementDetailMapper bussMaterialProcurementDetailMapper;

    @Autowired
    private PackageStorageMaterialMapper packageStorageMaterialMapper;

    @Autowired
    private BussStorageNoticeMaterialMapper bussStorageNoticeMaterialMapper;

    @Autowired
    private BussStorageNoticeMapper bussStorageNoticeMapper;

    @Override
    public void addNumProcurement(PackageStorageMaterial packageStorageMaterial) {
        PackageStorageMaterial sourcePackageStorageMaterial = packageStorageMaterialMapper.selectById(packageStorageMaterial.getId());
        BussMaterialProcurementDetail bussMaterialProcurementDetail = bussMaterialProcurementDetailMapper.selectById(packageStorageMaterial.getProcurementDetailId());
        this.checkProcurementNum(bussMaterialProcurementDetail,sourcePackageStorageMaterial,packageStorageMaterial);
        if(sourcePackageStorageMaterial ==  null){
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().add(packageStorageMaterial.getNum()));
        }else{
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().subtract(sourcePackageStorageMaterial.getNum()).add(bussMaterialProcurementDetail.getArrivalNum()));
        }
        bussMaterialProcurementDetailMapper.updateById(bussMaterialProcurementDetail);
    }

    @Override
    public void subtractNumProcurement(List<PackageStorageMaterial> list){
        for (PackageStorageMaterial packageStorageMaterial : list) {
            BussMaterialProcurementDetail bussMaterialProcurementDetail = this.bussMaterialProcurementDetailMapper.selectById(packageStorageMaterial.getProcurementDetailId());
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().subtract(packageStorageMaterial.getNum()));
            if(bussMaterialProcurementDetail.getArrivalNum().compareTo(BigDecimal.ZERO) == 0){
                bussMaterialProcurementDetail.setStatus(0);
            } else {
                bussMaterialProcurementDetail.setStatus(2);
            }
            this.bussMaterialProcurementDetailMapper.updateById(bussMaterialProcurementDetail);
        }
    }

    /**
     * 检查采购数量-包材
     * @param bussMaterialProcurementDetail 采购明细
     * @param sourcePackageStorageMaterial 原
     * @param packageStorageMaterial 新
     */
    private void checkProcurementNum(BussMaterialProcurementDetail bussMaterialProcurementDetail,PackageStorageMaterial sourcePackageStorageMaterial,PackageStorageMaterial packageStorageMaterial){
        BigDecimal subtract = BigDecimal.ZERO;
        if(sourcePackageStorageMaterial == null){
             subtract = bussMaterialProcurementDetail.getProcurementNum().subtract(bussMaterialProcurementDetail.getArrivalNum())
                    .subtract(packageStorageMaterial.getNum());
        }else {
            // 采购数量：10   到货数量：5（4+1） 原 4  新 2
            subtract = bussMaterialProcurementDetail.getProcurementNum()
                    .subtract(bussMaterialProcurementDetail.getArrivalNum().subtract(sourcePackageStorageMaterial.getNum()))
                    .subtract(packageStorageMaterial.getNum());
        }
        if(subtract.compareTo(BigDecimal.ZERO) < 0){
            throw new RuntimeException("入库单数量不能大于采购数量");
        }else if(subtract.compareTo(BigDecimal.ZERO) == 0){
            bussMaterialProcurementDetail.setStatus(1);
        }else{
            bussMaterialProcurementDetail.setStatus(2);
        }
    }

    @Override
    public void addNumProcurementAtNotice(BussStorageNoticeMaterial bussStorageNoticeMaterial) {
        BussStorageNoticeMaterial source = bussStorageNoticeMaterialMapper.selectById(bussStorageNoticeMaterial.getId());
        BussMaterialProcurementDetail bussMaterialProcurementDetail = bussMaterialProcurementDetailMapper.selectById(bussStorageNoticeMaterial.getProcurementDetailId());
        this.checkProcurementNumAtNotice(bussMaterialProcurementDetail,source,bussStorageNoticeMaterial);
        if(source ==  null){
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().add(bussStorageNoticeMaterial.getActualNum()));
        }else {
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().subtract(source.getActualNum()).add(bussMaterialProcurementDetail.getArrivalNum()));
        }
        bussMaterialProcurementDetailMapper.updateById(bussMaterialProcurementDetail);
    }

    @Override
    public void subtractNumProcurementAtNotice(List<BussStorageNoticeMaterial> list) {
        for (BussStorageNoticeMaterial bussStorageNoticeMaterial : list) {
            BussMaterialProcurementDetail bussMaterialProcurementDetail = this.bussMaterialProcurementDetailMapper.selectById(bussStorageNoticeMaterial.getProcurementDetailId());
            bussMaterialProcurementDetail.setArrivalNum(bussMaterialProcurementDetail.getArrivalNum().subtract(bussStorageNoticeMaterial.getActualNum()));
            if(bussMaterialProcurementDetail.getArrivalNum().compareTo(BigDecimal.ZERO) == 0){
                bussMaterialProcurementDetail.setStatus(0);
            } else {
                bussMaterialProcurementDetail.setStatus(2);
            }
            this.bussMaterialProcurementDetailMapper.updateById(bussMaterialProcurementDetail);
        }
    }

    /**
     *
     * @param bussMaterialProcurementDetail
     * @param source
     * @param noticeMaterial
     */
    private void checkProcurementNumAtNotice(BussMaterialProcurementDetail bussMaterialProcurementDetail,BussStorageNoticeMaterial source,BussStorageNoticeMaterial noticeMaterial){
        BigDecimal subtract = BigDecimal.ZERO;
        if(source == null){
            subtract = bussMaterialProcurementDetail.getProcurementNum().subtract(bussMaterialProcurementDetail.getArrivalNum())
                    .subtract(noticeMaterial.getActualNum());
        }else {
            // 采购数量：10   到货数量：5（4+1） 原 4  新 2
            subtract = bussMaterialProcurementDetail.getProcurementNum()
                    .subtract(bussMaterialProcurementDetail.getArrivalNum().subtract(source.getActualNum()))
                    .subtract(noticeMaterial.getActualNum());
        }
        if(subtract.compareTo(BigDecimal.ZERO) < 0){
            throw new RuntimeException("入库单数量不能大于采购数量");
        }else if(subtract.compareTo(BigDecimal.ZERO) == 0){
            bussMaterialProcurementDetail.setStatus(1);
        }else {
            bussMaterialProcurementDetail.setStatus(2);
        }
    }



}
